package com.example.my_web.controller;

import com.example.my_web.pojo.User;
import com.example.my_web.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public String login(User user) {
        return "/user/login";
    }

    @RequestMapping("/loginCheck")
    public String loginCheck(User user, Model model) {
        User realUser = userService.selectUserByName(user.getName());
        if(realUser == null) realUser = new User();
        System.out.println(realUser);
        if(realUser.equals(user)) {
            return "/user/home";
        }
        else {
            model.addAttribute("msg","用户名或密码错误");
            // 重定向会将参数同时传过去
            return "redirect:/user/login";
        }
    }
}
